package com.siwei.apply.service.impl;

import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.res.*;
import com.siwei.apply.domain.vo.ProjectFilterVo;
import com.siwei.apply.domain.vo.ProjectUpdateVo;
import com.siwei.apply.domain.vo.ProjectVo;
import com.siwei.apply.mapper.ProjectMapper;
import com.siwei.apply.mapper.ProjectWorkflowMapper;
import com.siwei.apply.mapper.TdgyMapper;
import com.siwei.apply.mapper.WorkflowMapper;
import com.siwei.apply.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.siwei.apply.common.Common.UserId;

/**
 * 项目服务实现类
 */
@Service
public class ProjectImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private TdgyMapper tdgyMapper;
    @Autowired
    private ProjectWorkflowMapper projectWorkflowMapper;
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public String add(ProjectVo projectVo) {
        Project project = new Project();
        project.generateId();
        project.setName(projectVo.getName());
        project.setCode(projectVo.getCode());
        project.setCompany(projectVo.getCompany());
        project.setProjectType(projectVo.getProjectType());
        project.setCreatorId(UserId);
        projectMapper.add(project);

        return project.getId();
    }

    @Override
    public Project get(String projectId) {
        return projectMapper.get(projectId);
    }

    @Override
    public Map<String, Object> getList(ProjectFilterVo projectFilterVo) {
        List<Project> projects = projectMapper.getList(projectFilterVo);
        Integer count = projectMapper.getCount(projectFilterVo);

        Map<String, Object> map = new HashMap<>();
        map.put("projects", projects);
        map.put("count", count);
        return map;
    }


    @Override
    public void update(ProjectUpdateVo projectUpdateVo) {
        projectMapper.update(projectUpdateVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("IDs list cannot be null or empty");
        }

        // 验证每个ID都不为空
        for (String id : ids) {
            if (id == null || id.trim().isEmpty()) {
                throw new IllegalArgumentException("ID cannot be null or empty");
            }
        }

        projectMapper.batchDelete(ids);
    }

    @Override
    public ProjectOverviewRes getOverview(Integer projectType) {
        ProjectOverviewRes projectOverviewRes = new ProjectOverviewRes();
        // todo 根据人员/权限统计
        // 获取项目数量
        ProjectNumRes projectNumRes = projectMapper.countTypeStats();
        projectOverviewRes.setProjectNum(projectNumRes);
        // 获取项目供应
        List<ProjectSupplyRes> projectSupplyList = tdgyMapper.countAndSumByGdType(projectType);
        projectOverviewRes.setProjectSupplyList(projectSupplyList);
        // 获取项目进度
        List<ProjectProcessRes> projectProcessList = projectWorkflowMapper.countMaxWorkflowByName(projectType);
        projectOverviewRes.setProjectProcessList(projectProcessList);
        return projectOverviewRes;
    }

    @Override
    public List<ProjectCycleRes> getCycle(String projectId) {
        Integer projectType = projectMapper.getProjectTypeById(projectId);
        // 获取不同类型的流程
        List<ProjectCycleRes> workflows = workflowMapper.selectByProjectTypeOrderByIndex(projectType);
        // 获取项目对应的工作流
        List<ProjectWorkflowRes> projectWorkflowsAll = projectWorkflowMapper.selectCycleByProjectId(projectId);
        // 过滤掉没有上链的
        List<ProjectWorkflowRes> projectWorkflows = new ArrayList<>();
        for (ProjectWorkflowRes projectWorkflowRes : projectWorkflowsAll) {
            Boolean hasOnchain = projectWorkflowMapper.isOnchain(projectWorkflowRes.getNodeId(), projectId, projectWorkflowRes.getNodeTableName());
            if (hasOnchain) projectWorkflows.add(projectWorkflowRes);
        }

        Map<String, List<ProjectWorkflowRes>> workflowMap = new HashMap<>();
        // 将项目工作流按节点表名分组
        for (ProjectWorkflowRes projectWorkflow : projectWorkflows) {
            String workflowId = projectWorkflow.getWorkflowId();
            if (!workflowMap.containsKey(workflowId)) {
                workflowMap.put(workflowId, new ArrayList<>());
            }
            workflowMap.get(workflowId).add(projectWorkflow);
        }
        // 构建项目周期响应
        for (ProjectCycleRes workflow : workflows) {
            if (workflowMap.containsKey(workflow.getId())) {
                workflow.setHasOnchain(true);
                workflow.setWorkflows(workflowMap.get(workflow.getId()));
            } else {
                workflow.setWorkflows(new ArrayList<>());
            }
        }
        return workflows;
    }
}

